﻿using System.Collections.Generic;
using Engine;
using Engine.Graphics;
using Game;
namespace Mekiasm
{
    public class SubsystemSingleChestDraw
    {
        public static LabelWidget labelWidget = new LabelWidget();
        public static PrimitivesRenderer3D primitivesRenderer3D2 = new PrimitivesRenderer3D();
        public static PrimitivesRenderer3D primitivesRenderer3D = new PrimitivesRenderer3D();
        public static PrimitivesRenderer2D primitivesRenderer2D = new PrimitivesRenderer2D();
        public static List<Point3> RenderList = new List<Point3>();
        public static RenderTarget2D renderTarget2D = new RenderTarget2D(256, 256, 1, ColorFormat.Rgba8888, DepthFormat.None);
        public static Matrix matrix = Matrix.Identity;
        public static TerrainGeometrySubset[] terrainGeometrySubsets;
        public static void Add(int x, int y, int z)
        {
            RenderList.Add(new Point3(x, y, z));
        }
        public static void OnChunkDiscarding(TerrainChunk chunk)
        {
            List<Point3> toRemove = new List<Point3>();
            for (int i = 0; i < RenderList.Count; i++)
            {
                Point3 drawItem = RenderList[i];
                if (Terrain.ToChunk(drawItem.X, drawItem.Z) == chunk.Coords)
                {
                    toRemove.Add(drawItem);
                }
            }

            for (int i = 0; i < toRemove.Count; i++)
            {
                var point = toRemove[i];
                Remove(point.X, point.Y, point.Z, chunk.GetCellValueFast(point.X, point.Y, point.Z));
            }

        }

        public static void Remove(int x, int y, int z, int value)
        {
            Point3 point = new Point3(x, y, z);
            RenderList.Remove(point);
        }

        public static void Dispose()
        {
            RenderList.Clear();
        }
        public static void Draw(Camera camera)
        {
            for (int j = 0; j < RenderList.Count; j++)
            {
                Point3 renderPoint = RenderList[j];
                int value = ILibrary.SubsystemTerrain.Terrain.GetCellValueFast(renderPoint.X, renderPoint.Y, renderPoint.Z);
                int direction = ILibrary.GetDirection(value);
                SaveItemSystem.GetBlockData(value, out Common.SaveItem toolSaveItem);
                if (toolSaveItem == null || toolSaveItem.GetSlotCount(0) == 0)
                {
                    continue;
                }
                float s = LightingManager.LightIntensityByLightValue[Terrain.ExtractLight(value)];
                Color color = new Color(s, s, s);
                RenderTarget2D renderTarget = Display.RenderTarget;
                Display.RenderTarget = renderTarget2D;
                Display.Clear(Color.Transparent);
                string numstr = toolSaveItem.GetSlotCount(0).ToString();
                FontBatch2D fontBatch = primitivesRenderer2D.FontBatch(labelWidget.Font, 0, DepthStencilState.None, null, BlendState.Opaque, SamplerState.PointClamp);
                int id = Terrain.ExtractContents(toolSaveItem.GetSlotValue(0));
                Block block = BlocksManager.Blocks[id];
                DrawBlockEnvironmentData blockEnvironmentData = new DrawBlockEnvironmentData();
                Matrix m2 = Matrix.CreateLookAt(block.GetIconViewOffset(toolSaveItem.GetSlotValue(0), blockEnvironmentData), Vector3.Zero, Vector3.UnitY);
                m2 *= Matrix.CreateOrthographic(3.6f, 3.6f, -10f, 10f);
                m2 *= Matrix.CreateTranslation(0f, 0.2f, 0f);
                blockEnvironmentData.ViewProjectionMatrix = m2;
                block.DrawBlock(primitivesRenderer3D, toolSaveItem.GetSlotValue(0), color, 0.8f, ref matrix, blockEnvironmentData);
                fontBatch.QueueText($"{numstr}", new Vector2(128, 180), 0f, color, TextAnchor.HorizontalCenter, new Vector2(2), Vector2.Zero);//不是纹理坐标				
                primitivesRenderer3D.Flush(matrix);
                primitivesRenderer2D.Flush();
                Display.RenderTarget = renderTarget;
                MekModLoader.DrawCubeFaceOverlay(camera, renderPoint, direction, Color.Transparent, null, renderTarget2D);
            }            
        }
    }
}
